home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / applets / clock2 / beta / needle.jav < prev   
Encoding:
Text File  |  1995-12-15  |  4.1 KB  |  121 lines

  1. /* ----------------------------------------------------------------
  2.  * Needle 1.0, Copyright (c) 1995 Nils Hedstrom, All Rights Reserved.
  3.  * Permission to use, copy, modify, and distribute this software and its
  4.  * documentation for NON-COMMERCIAL purposes and without fee is hereby
  5.  * granted provided that this copyright notice and appropiate documention
  6.  * appears in all copies.
  7.  * 
  8.  * NILS HEDSTROM MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
  9.  * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT
  10.  * LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  11.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. NILS HEDSTROM SHALL NOT BE LIABLE
  12.  * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  13.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  14.  * 
  15.  * For documentation look at http://www-und.ida.liu.se/~d94nilhe/java/applets.html 
  16.  * 
  17.  * This class creates and manages Needles.
  18.  * */
  19.  
  20. import java.awt.Graphics;
  21. import java.awt.Color;
  22. import java.lang.Math;
  23.  
  24. /** 
  25. This maintain a Needle (or a hand).
  26. @version     1.0 29 November 1995
  27. @author         <A HREF="http://www.edu.isy.liu.se/~d94nilhe/">Nils Hedstr÷m</A>
  28. */
  29.  
  30. public class Needle
  31. {
  32.   private int centerx, centery, lengthx, lengthy,width,type;
  33.   private double angle;
  34.   private Color color,border;
  35.   
  36.   /**
  37.    * Constructs a needle.
  38.    * @param in_color          The color
  39.    * @param in_border         The color of the border (the border is 1 pixel wide)
  40.    * @param in_type           The type (0=no Needle, 1=Triangle, 2=Rectangle, 3=Romb)
  41.    * @param in_width The witdh
  42.    * @param in_centerx The x-cord of the center
  43.    * @param in_centery The y-cord of the center
  44.    * @param in_lengthx The length in the x-axis
  45.    * @param in_lengthy The lenght in the y-axis
  46.    * @param in_angle The angle (0=To the right, pi=To the left)
  47.   */
  48.   public Needle(Color in_color, Color in_border,int in_type,int in_width,int in_centerx, int in_centery, int in_lengthx, int in_lengthy, double in_angle)
  49.     {
  50.       color=in_color;                 // The color
  51.       border=in_border;               // The color of the border (1 pixel width)
  52.       type=in_type;                   // The type (0=no Needle, 1=Triangle, 2=Rectangle, 3=Romb)
  53.       width=in_width;                 // The witdh
  54.       centerx=in_centerx;             // The x-cord of the center
  55.       centery=in_centery;             // The y-cord of the center
  56.       lengthx=in_lengthx;             // The length in the x-axis
  57.       lengthy=in_lengthy;             // The lenght in the y-axis
  58.       angle=in_angle;                 // The angle (0=To the right, pi=To the left)
  59.       
  60.     }
  61.   /**
  62.    * Sets the angle of the needle.
  63.    * @param in_angle          The needle's new angle. (0=To the right, pi/2 Up, pi=Left, 3pi/2 Down)
  64.    */
  65.   
  66.   public void setAngle(double in_angle)
  67.     {
  68.       angle=in_angle;
  69.     }
  70.   /**
  71.    * Draws the needle on a graphics object.
  72.    * @param g         The graphics object which the needle will be drawn upon.
  73.    */
  74.   public void drawNeedle(Graphics g)
  75.     {
  76.       int x,y,dx,dy;
  77.       double len;
  78.       x=centerx+(int)(lengthx*Math.cos(angle));
  79.       y=centery+(int)(lengthy*Math.sin(angle));
  80.       len=Math.sqrt(Math.pow(x-centerx,2)+Math.pow(y-centery,2));
  81.       dx=(int)(width*(centery-y)/len);
  82.       dy=(int)(width*(x-centerx)/len);
  83.       if(width==1) g.drawLine(centerx,centery,x,y);
  84.       else 
  85.     {
  86.       switch (type)
  87.         {
  88.         case 0:
  89.           break;
  90.         case 1:
  91.           draw_poly(centerx-dx,centerx+dx,x,x,centery-dy,centery+dy,y,y,g);
  92.           break;
  93.         case 2:
  94.           draw_poly(centerx-dx,centerx+dx,x+dx,x-dx,centery-dy,centery+dy,y+dy,y-dy,g);
  95.           break;
  96.         case 3:
  97.           draw_poly(centerx,(centerx+x)/2+dx,x,(centerx+x)/2-dx,centery,(centery+y)/2+dy,y,(centery+y)/2-dy,g);
  98.           break;
  99.         }
  100.     } 
  101.     }
  102.   
  103.   private void draw_poly(int x1,int x2,int x3,int x4,int y1,int y2,int y3,int y4,Graphics g)
  104.     {
  105.       int x[]={x1,x2,x3,x4,x1},y[]={y1,y2,y3,y4,y1};
  106.       g.setColor(color);
  107.       g.fillPolygon(x,y,5);
  108.       g.setColor(border);
  109.       g.drawPolygon(x,y,5);
  110.       
  111.     }  
  112. }
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.